package com.zjj.concurrent.thread;

import java.util.Random;
import java.util.concurrent.*;

/**
 * @author zhanglei.zjj
 * @description 使用有返回值的Callable接口创建线程
 * @date 2023/5/14 21:58
 */
public class TestThreadUseCallable {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Callable<Integer> callable = new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                return new Random().nextInt(10);
            }
        };
        ExecutorService executorService = Executors.newFixedThreadPool(1);
        Future<Integer> future = executorService.submit(callable);
        System.out.println(future.get());

        // 比如我们主动调 call() 方法，需要处理 受检异常
        try {
            callable.call();
        } catch (Exception e) {
            e.printStackTrace();
        }
//        callable.call();
    }
}
